package com.yao.koin.ui

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.liveData
import androidx.lifecycle.viewModelScope
import com.yao.koin.base.BaseViewModel
import com.yao.koin.bean.BannerChild
import com.yao.koin.bean.DataX
import com.yao.koin.repository.HomeRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import com.yao.koin.util.Result


class ActivityViewModel(private val homeRepository: HomeRepository): BaseViewModel() {

    private val _text = MutableLiveData<String>().apply {
        value = "This is home Fragment"
    }
    val text: LiveData<String> = _text

    sealed class GirlType {
        object Home : GirlType()                 // 首页
    }

    private val _uiState = MutableLiveData<GirlModel>()
    val uiState: LiveData<GirlModel>
        get() = _uiState

    private var currentPage = 1

    val mBanners: LiveData<List<BannerChild>> = liveData {
        kotlin.runCatching {
            val data = homeRepository.getBanner()
            if (data is Result.Success) emit(data.data)
        }
    }


    val refreshHome: ()-> Unit = {getHomeGirlList(true)}

    fun getHomeGirlList(isRefresh: Boolean = false) = getGirlList(GirlType.Home, isRefresh)


    private fun getGirlList(articleType: GirlType, isRefresh: Boolean = false, cid: Int = 0) {
        viewModelScope.launch(Dispatchers.Main) {
            emitArticleUiState(true)

//            if (isRefresh) currentPage = if (articleType is GirlType.ProjectDetailList) 1 else 0

            try{

                val result = when (articleType) {
                    GirlType.Home -> homeRepository.getGridList(currentPage)
                }

                if (result is Result.Success) {
                    val gridList = result.data

                    if (currentPage>10) {
                        emitArticleUiState(showLoading = false, showEnd = true)
                        return@launch
                    }
                    currentPage++
                    emitArticleUiState(showLoading = false, showSuccess = gridList, isRefresh = isRefresh)

                } else if (result is Result.Error) {
                    emitArticleUiState(showLoading = false, showError = result.exception.message)
                }
//

            }catch(e:Exception){
                System.out.print(e.printStackTrace())
            }



        }
    }


    private fun emitArticleUiState(
        showLoading: Boolean = false,
        showError: String? = null,
        showSuccess: List<DataX>? = null,
        showEnd: Boolean = false,
        isRefresh: Boolean = false,
        needLogin: Boolean? = null
    ) {
        val uiModel = GirlModel(showLoading, showError, showSuccess, showEnd, isRefresh, needLogin)
        _uiState.value = uiModel
    }



    data class GirlModel(
        val showLoading: Boolean,
        val showError: String?,
        val showSuccess:  List<DataX>??,
        val showEnd: Boolean, // 加载更多
        val isRefresh: Boolean, // 刷新
        val needLogin: Boolean? = null
    )



}